#!/usr/bin/env ruby
# Creates a timeline for the various solutions for the determining 
# longitude at sea.
#  Created by sjavey on 2007-07-16.
#  Copyright (c) 2007. All rights reserved.

class History
  
  def initialize
    @license = "E1EWK3SY2-DHLATYPWU28RU1PSTILK"
    @years = [1514,1530,1559,1598,1618,1656,1675,1664,1675,1714,1737,1741,1759,1760,1770]
    @x_offset = 100
    @y_offset = 100
    @width = 600
    @height = 150
    @x_axis_steps = @years.size
  end
  
  def writeAxisValueText
    str = "<axis_value_text>"
    @years.each { |year| 
      str << "\n<string>#{year}</string>"
    }
    return str << "\n</axis_value_text>"
  end
  
  def yearToPixel(year)
    return @x_offset + (@width/@x_axis_steps)*@years.index(year)
  end
  
  def addEvent(year, label, color, y)
    str = <<EOS
    <text transition='drop' delay='0' duration='1.5' color='000000' alpha='70' size='9' x='#{yearToPixel(year)+7}' y='#{y}'>#{label}</text>
    <line transition='slide_right' delay='0' duration='0.5' x1='#{yearToPixel(year)}' y1='#{y+7}' x2='#{yearToPixel(year)}' y2='#{@y_offset+@height}' line_color='#{color}' line_thickness='2' line_alpha='75' />
    <circle transition='slide_right' delay='0' duration='0.5' x='#{yearToPixel(year)}' y='#{y+7}' radius='5' fill_color='#{color}' fill_alpha='90' line_thickness='0' />
EOS
  end
  
  def chart
    str = <<EOS
    <chart>
    <license>#{@license}</license>
    <axis_category size='11' color='ffffff' alpha='95'  />
    <axis_ticks value_ticks='true' category_ticks='true' major_thickness='1' minor_thickness='0' minor_count='0' major_color='222222' minor_color='222222' position='centered' />
    <axis_value size='8' color='000000' alpha='60' steps='#{@x_axis_steps}' min='0' max='#{@x_axis_steps}' />

    #{writeAxisValueText()}

    <chart_border color='000088' top_thickness='0' bottom_thickness='0' left_thickness='0' right_thickness='0' />
    
    <chart_data>
    <row>
    <string></string>
    <string>Moons 
    of Jupiter</string>
    <string>Motion
     of Moon</string>
    <string>Chronometer</string>
    </row>
    
    <row>
    <number>0</number>
    <number>#{@years.index(1618)}</number>
    <number>#{@years.index(1675)}</number>
    <number>#{@years.index(1770)}</number>
    </row>

    <row>
    <number>0</number>
    <number>#{@years.index(1598)}</number>
    <number>#{@years.index(1514)}</number>
    <number>#{@years.index(1530)}</number>
    </row>
    </chart_data>
    
    <chart_grid_h alpha='20' color='000000' thickness='1' type='dashed' />
    <chart_grid_v alpha='10' color='ee6666' thickness='0' />

    <chart_rect x='100' y='100' width='#{@width}' height='#{@height}' positive_color='ffffff' positive_alpha='100' />
    <chart_transition type='slide_left' delay='0' duration='5.6' order='all' />
    <chart_type>floating bar</chart_type>

    <draw>
    <rect layer='background' transition='slide_right' delay='0' duration='0.5' x='25' y='100' width='75' height='150' fill_color='000000' fill_alpha='20' line_thickness='0' />
    <text transition='drop' delay='0' duration='7' color='ffffaa' alpha='75' size='36' x='0' y='65' width='400' height='240' h_align='center'>history of  longitude</text>
    #{addEvent(1530, "Frisius - idea", "ffff00", 100)}    
    #{addEvent(1656, "Huygens - 1st clock", "ffff00", 100)}    
    #{addEvent(1664, "Huygens - 2nd clock", "ffff00", 115)}    
    #{addEvent(1737, "H-1", "ffff00", 108)}
    #{addEvent(1741, "H-2", "ffff00", 123)}
    #{addEvent(1759, "H-3", "ffff00", 138)}
    #{addEvent(1760, "H-4", "000000", 100)}
    #{addEvent(1770, "H-5", "00ffff", 123)}
    </draw>
    
    <legend_rect x='-100' y='-100' width='5' height='5' />

    <series_color>
    <color>ffffff</color>
    <color>aa88ff</color>
    <color>aaff88</color>
    </series_color>
    
    <series_gap set_gap='50' bar_gap='5' />
    <series_switch>true</series_switch>

    </chart>
EOS
  end
end

history = History.new()
puts history.chart()

